package user_local_ewallet

import (
	"context"
	"errors"

	"gitee.com/liuxuezhan/ar-platform/assets-api/pkg/xerrors"
	"gitee.com/liuxuezhan/ar-platform/base-common/headInfo"
	"gitee.com/liuxuezhan/ar-platform/rpc-client/assetsClient/rpc"
	"github.com/jinzhu/copier"

	"gitee.com/liuxuezhan/ar-platform/assets-api/internal/svc"
	"gitee.com/liuxuezhan/ar-platform/assets-api/internal/types"

	"github.com/zeromicro/go-zero/core/logx"
)

type UserLocalEWalletListLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

// 用户提现页面--本地电子钱包--列表
func NewUserLocalEWalletListLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UserLocalEWalletListLogic {
	return &UserLocalEWalletListLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *UserLocalEWalletListLogic) UserLocalEWalletList() (resp *types.UserLocalEwalletInfoResponse, err error) {

	request := rpc.UserLocalEwalletInfoRequest{}
	err = copier.Copy(&request, &types.UserLocalEwalletInfoRequest{})
	if err != nil {
		return nil, xerrors.ErrDataConvert
	}
	request.UserId = headInfo.GetTokenUid(l.ctx)
	if request.UserId == 0 {
		return &types.UserLocalEwalletInfoResponse{
			DataList: []types.UserLocalEwalletInfo{},
		}, errors.New("user is not exist")
	}
	page, err := l.svcCtx.AssetsRpc.UserChannel.UserLocalEWalletsInfo(l.ctx, &request)
	var dataList []types.UserLocalEwalletInfo

	if err != nil {
		return &types.UserLocalEwalletInfoResponse{
			DataList: dataList,
		}, err
	}
	if len(page.UserLocalEwalletInfoList) == 0 {
		return &types.UserLocalEwalletInfoResponse{
			DataList: dataList,
		}, nil
	}
	err = copier.Copy(&dataList, page.UserLocalEwalletInfoList)
	if err != nil {
		return nil, xerrors.ErrDataConvert
	}
	return &types.UserLocalEwalletInfoResponse{
		DataList: dataList,
	}, nil
}
